--
-- Author: mm
-- Date: 2018-05-07 18:07:08
--
-- GameplaypreviewController
--

local GameplaypreviewView = import("..views.GameplaypreviewView")

local panelTypeNum = 3 -- 页面类型个数 = level_preview.pageType

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("GameplaypreviewModel")
    self._curPageIdx = 1 -- 默认在第一页
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
end

function ClassRef:onEvent(eventName, data)
end

function ClassRef:listEvent()
    return {}
end


function ClassRef:openView(data)
    self._curPageIdx = self:_parseInitPage()

    if cc.isDead(self._view) then
        self._view = GameplaypreviewView.new( app.mvc.VIEWTYPE_BLUR )
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        self:addChild(self._view)
    end
    self._view:show()
    self._view:openAction(true)
    self:_selectedPage()
end
function ClassRef:clearView()
    if self._view then
        self._view = nil
        self._curPageIdx = 1
    end
end

-- TODO，预览功能居然不是根据funcId，而是根据任务和等级来开放
function ClassRef:getDesktopIconPath()
    local pageIdx = self:_parseInitPage()
    if not pageIdx then return "" end
    local level_previewConf = GD:getGamePlayPreViewConfById( pageIdx )
    if not level_previewConf then return "" end
    
    local icontPath = ""
    local displayConf = level_previewConf.display
    if displayConf and displayConf ~= "" then
        if string.find( displayConf, ";" ) then
            local displayList = string.split( displayConf, ";" )
            for i,v in ipairs( displayList ) do
                if string.find( v, "|" ) then
                    local tempList = string.split( v, "|" )
                    if UD:getClass() == tonumber( tempList[1] ) then -- 自己职业？
                        icontPath = Res.gamePlayPath .. tempList[2] .. ".png"
                        break
                    end
                else
                    local idx = 0
                    for i2 = 1, 3 do
                        local unLockList = string.split( level_previewConf[ "unlock" .. i2 ], "|" )
                        if 1 == tonumber( unLockList[1] ) then -- 等级？
                            if UD:getLevel() < tonumber( unLockList[2] ) then
                                idx = i2
                                break
                            end
                        elseif 2 == tonumber( unLockList[1] ) then -- 任务?
                            if not UD:isMainTaskFinished( unLockList[2] ) then
                                idx = i2
                                break
                            end
                        end
                    end
                    if 0 ~= idx then
                        icontPath = Res.gamePlayPath .. displayList[ idx ] .. ".png"
                        break
                    end
                end
            end
        else
            icontPath = Res.gamePlayPath .. displayConf .. ".png"
        end
    end
    return icontPath
end

function ClassRef:isFuncPreviewOver()
    local isOver = false
    local pageIdx = self:_parseInitPage()
    local confs = GD:getGamePlayPreViewConfs()
    if pageIdx >= #confs then -- 最大边界？
        local maxLevelConf = confs[pageIdx]
        local myLevel = UD:getLevel()
        local maxLv = 0
        local hasTaskFlag = false -- 是否依赖任务、默认false
        local taskFinished = false -- 任务完成标记、默认false
        for i = 1, 3 do
            local unLockList = string.split( maxLevelConf[ "unlock" .. i ], "|" )
            if 1 == tonumber( unLockList[1] ) then -- 等级？
                maxLv = math.max( maxLv, tonumber( unLockList[2] ) )
            elseif 2 == tonumber( unLockList[1] ) then -- 任务?
                hasTaskFlag = true
                taskFinished = taskFinished or UD:isMainTaskFinished( unLockList[2] )
            end
        end
        if hasTaskFlag then -- 依赖任务?
            if taskFinished then -- 依赖的任务全完成？
                if 0 < maxLv then -- 存在等级开放条件？
                    if maxLv <= myLevel then -- 等级超过最大值
                        isOver = true
                    end
                else
                    isOver = true
                end
            end
        else -- 不依赖任务
            if 0 < maxLv then -- 存在等级开放条件？
                if maxLv <= myLevel then -- 等级超过最大值
                    isOver = true
                end
            end
        end
    end
    return isOver
end


-------------------------------------------------------
-- 根据玩家等级定位打开时候的指向页
function ClassRef:_parseInitPage()
    local pageIdx = 0
    local myLevel = UD:getLevel()
    local logicLeveol = myLevel * 10
    local confs = self._model:getGamePlayInfos()
    for i = 1, #confs do
        local j = i + 1 -- 下一行索引
        if j > #confs then -- 下一行索引越界？
            pageIdx = i
            break
        else
            local curConf = confs[i]
            local curPageLevel = tonumber( curConf.pageLevel )
            local nextConf = confs[j]
            local nextPageLevel = tonumber( nextConf.pageLevel )
            if 1 == i then -- 第一行
                if logicLeveol < curPageLevel then
                    pageIdx = i
                    break
                end
            else
                if logicLeveol < curPageLevel then
                    pageIdx = i
                    break
                elseif curPageLevel <= logicLeveol and logicLeveol < nextPageLevel then
                    pageIdx = j
                    break
                end
            end
        end
    end
    -- release_print( " ---------------->>>> 解析出来定位到的页面 ", pageIdx )
    return pageIdx
end

-- 指定页数
function ClassRef:_selectedPage( pageIdx )
    if not self._view:isVisible() then return end
    if pageIdx and self._curPageIdx ~= pageIdx then
        self._curPageIdx = pageIdx
    end
    self._view:refreshContent( self._model:getGamePlayInfoByIdx( self._curPageIdx ) )
    self._view:setLeftBtnVisible( 1 < self._curPageIdx )
    self._view:setRightBtnVisible( self._curPageIdx < self._model:getGamePlayInfoMaxNum() )
end

-------------------------------------------------------
function ClassRef:onOperate(op, data)
    -- print("GameplaypreviewController:onOperate op = " .. op)
    if op == "goto_leftPage" then -- 左切页
        if 1 >= self._curPageIdx then
            return
        end
        self._curPageIdx = self._curPageIdx - 1
        self:_selectedPage()
    elseif op == "goto_rightPage" then -- 右切页
        if self._model:getGamePlayInfoMaxNum() <= self._curPageIdx then
            return
        end
        self._curPageIdx = self._curPageIdx + 1
        self:_selectedPage()
    end
end

return ClassRef
